﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using S.Authorization.Common;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Authority;
using S.Authorization.Domain.Model.Organization;
using S.Authorization.Infrastructure.Interface.Organization;
using S.Authorization.Read.API.Application.Command.OrganizationCommand;

namespace S.Authorization.Read.API.Application.CommandHandlers.OrganizationCommandHandler
{
    public class DepartmentListCommandHandler : IRequestHandler<DepartmentListCommand, List<DtoCasCade>>,IRequestHandler<DepartMentShowCommand, List<SelectDto>>,IRequestHandler<DepartMentPageAllCommand,Pager<DepartmentModel>>
    {
        private readonly IDepartmentRepository department;
        public DepartmentListCommandHandler(IDepartmentRepository _department)
        {
            this.department = _department;
        }
        /// <summary>
        /// 级联 部门
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        private  List<DtoCasCade> GetDepartment(int menuId)
        {
            List<DepartmentModel> childList = department.GetAllAsync().Result.Where(x => x.PID == menuId).OrderByDescending(x=>x.SerialNumber).ToList();
            List<DtoCasCade> children = childList.Select(x => new DtoCasCade
            {
                value = x.DepartMentId,
                label = x.IndustryTitle,
                children = GetDepartment(x.DepartMentId).Count() == 0 ? null : GetDepartment(x.DepartMentId),
            }).ToList();
            return children;
        }
        /// <summary>
        /// 部门显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<DtoCasCade>> Handle(DepartmentListCommand request, CancellationToken cancellationToken)
        {
            return GetDepartment(request.PID);
        }
        /// <summary>
        /// 部门下拉框显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<SelectDto>> Handle(DepartMentShowCommand request, CancellationToken cancellationToken)
        {
            List<DepartmentModel> childList =await department.GetAllAsync();
            List<SelectDto> children = childList.Select(x => new SelectDto
            {
                value = x.DepartMentId,
                label = x.IndustryTitle,
            }).ToList();
            return children;
        }
        /// <summary>
        /// 部门表分页显示加组合查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Pager<DepartmentModel>> Handle(DepartMentPageAllCommand request, CancellationToken cancellationToken)
        {
            List<DepartmentModel> departList = await department.GetAllAsync();
            if (!string.IsNullOrEmpty(request.IndustryTitle))
            {
                departList = departList.Where(x => x.IndustryTitle.Contains(request.IndustryTitle)).ToList();
            }
            if(!string.IsNullOrEmpty(request.state.ToString()))
            {
                departList = departList.Where(x => x.IsDel==request.state).ToList();
            }
            int totalCount = departList.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            departList = departList.OrderBy(x => x.DepartMentId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            return new Pager<DepartmentModel>()
            {
                PageIndex=request.PageIndex,
                PageSize=request.PageSize,
                TotalCount = totalCount,
                PageCount = pageCount,
                resultData = departList
            };
        }
    }
}
